package com.example.java_chatroom.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class TestWebSocketController extends TextWebSocketHandler {

    //连接建立成功后执行这个方法
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("Test API 连接成功");
    }

    //websocket收到消息时自动调用
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        System.out.println("Test API 收到消息：" + message.toString());
        //实现回响操作
        session.sendMessage(message);
    }

    //连接异常时自动调用
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("Test API 连接异常");
    }

    //连接关闭时自动调用
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("Test API 连接关闭");
    }
}
